﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AdoHelper
{
    [Serializable]
    public class UpdateBuilder
    {
        private int? top = null;
        public int? Top
        {
            get { return top; }
            set { top = value; }
        }
        
        private string tableName = "";
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        private UpdateClause update = new UpdateClause();
        public UpdateClause Update
        {
            get { return update; }
        }

        private FromClause from = new FromClause();
        public FromClause From
        {
            get { return from; }
        }

        private WhereClause where = new WhereClause();
        public WhereClause Where
        {
            get { return where; }
        }


        public string Statement
        {
            get
            {
                string s = "";
                if(this.update.ToString()!="")
                {
                    s += "UPDATE ";
                    if (this.top.HasValue)
                    {
                        s += "TOP (" + this.top.Value + ") ";
                    }
                    s += this.tableName + " SET " + this.update.ToString() + "\r\n";
                }
                if (this.from.ToString() != "")
                {
                    s += "FROM " + this.from.ToString() + "\r\n"; ;
                }
                if (this.where.ToString() != "")
                {
                    s += "WHERE " + this.where.ToString() + "\r\n";
                }
                return s;
            }
        }

        public void Reset()
        {
            this.top = null;
            this.tableName = "";
            this.update.Clear();
            this.where.Clear();
            this.from.Clear();
        }

        public override string ToString()
        {
            return this.Statement;
        }

    }
    [Serializable]
    public class UpdateClause
    {
        private string totalExpression = "";
        private int count = 0;
        private int newlineLimit = 5;
        public int NewlineLimit
        {
            get { return newlineLimit; }
            set { newlineLimit = value; }
        }

        public void Add(params KeyValuePair<string, string>[] pairs)
        {
            for (int i = 0; i < pairs.Length; i++)
            {
                count++;
                if (totalExpression != "")
                {
                    totalExpression += ", ";
                }
                if (this.count == this.newlineLimit)
                {
                    this.count = 0;
                    this.totalExpression += "\r\n";
                }
                totalExpression += pairs[i].Key + " = " + pairs[i].Value;
            }
        }
        public void Clear()
        {
            this.count = 0;
            this.totalExpression = "";
        }
        public override string ToString()
        {
            return totalExpression;
        }

    }

}
